package com.party.admin.biz.enterpriseservice;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.enterpriseservice.EpCategory;
import com.party.core.service.enterpriseservice.IEpCategoryService;
import com.party.core.service.enterpriseservice.IEpServiceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: party
 * @description: 企业服务类型业务
 * @author: Hua Qunhao
 * @create: 2019-01-09 18:20
 */
@Service("EpServiceCategoryBizService")
public class EpServiceCategoryBizService {

    @Autowired
    private IEpCategoryService epCategoryService;
    @Autowired
    private IEpServiceCategoryService epServiceCategoryService;

    public List<EpCategory> getEpCategoryListAll() {
        return epCategoryService.list(new EpCategory());
    }

    public void updateGoodsNum(String sign, String serviceId, String newCateIdStr) {

        List<String> ids_new = Lists.newArrayList();

        if (StringUtils.isNotEmpty(newCateIdStr)) {
            ids_new.addAll(Arrays.asList(newCateIdStr.split(",")));
        }
        List<EpCategory> epCategoryList = epServiceCategoryService.queryCategoryListbyServiceId(serviceId);
        List<String> ids_old = LangUtils.transform(epCategoryList, new Function<EpCategory, String>() {
            @Override
            public String apply(EpCategory input) {
                return input.getId();
            }
        });

        if ("update".equalsIgnoreCase(sign)) {


            HashMap<String, String> hashMap = Maps.newHashMap();

            for (String str : ids_new) {
                hashMap.put(str, "");
            }
            for (String str : ids_old) {
                hashMap.put(str, "");
            }

            for (Map.Entry<String, String> entry : hashMap.entrySet()) {

                String key = entry.getKey();
                EpCategory epCategory = epCategoryService.get(key);

                Integer goodsNum = epCategory.getGoodsNum();
                //旧的包含,新的不包含------>删除
                if (ids_old.contains(key) && !ids_new.contains(key)) {
                    epCategory.setGoodsNum(goodsNum - 1);
                    epCategoryService.update(epCategory);
                }
                //新的包含,旧的不包含------>新增
                if (ids_new.contains(key) && !ids_old.contains(key)) {
                    epCategory.setGoodsNum(goodsNum + 1);
                    epCategoryService.update(epCategory);
                }
            }
        }

        if ("delete".equalsIgnoreCase(sign)) {
            for (String categoryId : ids_old) {
                EpCategory epCategory = epCategoryService.get(categoryId);
                epCategory.setGoodsNum(epCategory.getGoodsNum() - 1);
                epCategoryService.update(epCategory);
            }
        }
    }
}
